// src/modules/app/entities/user-relation.entity.ts
import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  JoinColumn,
  CreateDateColumn,
  UpdateDateColumn,
} from 'typeorm';
import { User } from '../../entities';
import { ApiHideProperty } from '@nestjs/swagger';
import { IsEnum, IsNumber, IsOptional, IsString } from 'class-validator';

export enum RelationType {
  /**
   * 配偶
   */
  SPOUSE = 'SPOUSE',
  /**
   * 父母
   */
  PARENT = 'PARENT',
  /**
   * 子女
   */
  CHILD = 'CHILD',
  /**
   * 兄弟姐妹
   */
  SIBLING = 'SIBLING',
  /**
   * 其他亲属
   */
  OTHER = 'OTHER',
}

/**
 * * 用户家属关联表
 */
@Entity('user_relations')
export class UserRelation {
  @PrimaryGeneratedColumn('increment')
  relationId: number;

  /**
   * 关联发起用户
   */
  @ManyToOne(() => User, (user) => user.relations, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'fromUserId' })
  fromUser?: User;

  /**
   * 关联目标用户
   */
  @ManyToOne(() => User, (user) => user.receivedRelations, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'toUserId' })
  toUser?: User;

  /**
   * 关联类型
   * SPOUSE: 配偶
   * PARENT: 父母
   * CHILD: 子女
   * SIBLING: 兄弟姐妹
   * OTHER: 其他亲属
   */
  @IsOptional()
  @IsEnum(RelationType)
  @Column({
    type: 'enum',
    enum: RelationType,
    default: RelationType.OTHER,
  })
  relationType?: RelationType;

  @CreateDateColumn()
  createdAt?: Date;

  @ApiHideProperty()
  createdBy?: number;

  @UpdateDateColumn()
  updatedAt?: Date;

  @ApiHideProperty()
  @Column({ default: false, select: false })
  isDeleted?: boolean;
}
